package com.controller.buying 
{
	import com.events.buying.EnquiryEvent;
	import com.model.valueObjects.VoEnquiryEmail;
	import com.model.valueObjects.VoEnquiryHeader;
	import com.service.EnquiryDetails;
	import com.view.Buying.com.EnquiryListPopup;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class EnquiryController extends AbstractController
	{
	
		[Inject]
		public var enquiryDetails:EnquiryDetails;
		
		public function EnquiryController()
		{
			super();
		}

		[EventHandler(event="EnquiryEvent.GET_ALL")]
		public function getAll():void{
		sh.executeServiceCall(enquiryDetails.getAllEnquires(),getAllResult, handleFault );
	
		}
		private function getAllResult(event:ResultEvent):void
		{
			if(model.currentMenu=='enquiryBtn')
			model.currentBuyTableIndex=0;
			
		model.enquiryList=event.result as ArrayCollection;
		CursorManager.removeBusyCursor();
		model.busyCurrsorOn=true;

		}
		
		[EventHandler(event="EnquiryEvent.GET_ALL_ENQ_SUP")]
		public function getAllEnqSup():void{
			sh.executeServiceCall(enquiryDetails.enquiryListPopupHeader(),getAllEnqSupRes, handleFault );
			
		}
		private function getAllEnqSupRes(event:ResultEvent):void
		{
		//	if(model.currentMenu=='enquiryBtn')
			//	model.currentBuyTableIndex=0;
			
			model.enquiryListPopupHeader=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			
		}
		
		[EventHandler(event="EnquiryEvent.GET_ENQ_LIST")]
		public function getEnqList():void{
			sh.executeServiceCall(enquiryDetails.enquiryListPopup(),getEnqListResult, handleFault );
			
		}
		private function getEnqListResult(event:ResultEvent):void
		{
			model.enquiryListPopup=event.result as ArrayCollection;
		}
		
		[EventHandler(event="EnquiryEvent.GET_ENQ_SUPPLIER")]
		public function getEnqSupplier():void{
			sh.executeServiceCall(enquiryDetails.enqSupplierData(),getEnqSupplierResult, handleFault );
			
		}
		private function getEnqSupplierResult(event:ResultEvent):void
		{
			model.enquirySupplierList =event.result as ArrayCollection;
		}
		
		[EventHandler(event="EnquiryEvent.GET_ENQ_HEADER")]
		public function getEnqHeader():void{
			sh.executeServiceCall(enquiryDetails.enqHeaderData(),getEnqHeaderResult, handleFault );
			
		}
		private function getEnqHeaderResult(event:ResultEvent):void
		{
			
			model.enquiryHeaderDetails =event.result as ArrayCollection;
		}
		
		[EventHandler(event="EnquiryEvent.GET_ENQUIRY_CODE",properties="enquireySerialNo")]
		public function getEnqHeaderCode(enquireySerialNo:Number):void{
			sh.executeServiceCall(enquiryDetails.getEnquiryByCode(enquireySerialNo),getEnquiryByCodeHeader, handleFault );
			
		}
		private function getEnquiryByCodeHeader(event:ResultEvent):void
		{
			
			model.enquiryDetail =event.result as VoEnquiryHeader;
		}
		
		[EventHandler(event="EnquiryEvent.ADD", properties="enquireyvo")]
		public function add(enquireyvo:VoEnquiryHeader):void
		{
			sh.executeServiceCall(enquiryDetails.addNewEnquiryHeader(enquireyvo), addResult, handleFault );	
		}
		
		private function addResult(event:ResultEvent):void
		{
			if(model.saveCloseBtn)model.currentBuyFormsIndex=0;	
			Alert.show( "Enquiry added" );	
			model.enqCode=event.result as String;
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			
		}
		
		[EventHandler(event="EnquiryEvent.SEND_EMAIL", properties="enquiryemailvo")]
		public function sendEmail(enquiryEmailVo:VoEnquiryEmail):void
		{
			sh.executeServiceCall(enquiryDetails.SendMail(enquiryEmailVo), sendMailResult, handleFault );	
		}
		
		private function sendMailResult(event:ResultEvent):void
		{
			Alert.show( "Mail Sent Successfully" );	
			CursorManager.removeBusyCursor();	
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="EnquiryEvent.UPDATE", properties="enquireyvo")]
		public function update(enquireyvo:VoEnquiryHeader):void
		{
			sh.executeServiceCall(enquiryDetails.editEnquiry(enquireyvo), editResult, handleFault );	
		}
		
		private function editResult(event:ResultEvent):void
		{
			getAll();
			if(model.saveCloseBtn)model.currentBuyFormsIndex=0;	
			Alert.show( "Enquiry Updated" );	
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="EnquiryEvent.DELETE", properties="enquireySerialNo")]
		public function deletef(enquireySerialNo:Number):void
		{
			sh.executeServiceCall(enquiryDetails.deleteEnquiry(enquireySerialNo),deleteResult, handleFault );	
		}		
		private function deleteResult(event:ResultEvent):void
		{
			model.currentBuyFormsIndex=0;
			getAll();
			Alert.show( "Enquiry Deleted");
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}